package cn.akwangl.spring.bean.definition;

import cn.akwangl.spring.ioc.model.User;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

/**
 * 注解方式注册Bean 元信息有以下几个方式
 * 1. @Component
 * 2. @Bean
 * 3. @Import
 * 4. XML方式
 * 5. JAVA API
 * 命名方式: {@link BeanDefinitionRegistry#registerBeanDefinition(String, BeanDefinition)}
 * 非命名方式: {@link org.springframework.beans.factory.support.BeanDefinitionReaderUtils#registerWithGeneratedName(AbstractBeanDefinition, BeanDefinitionRegistry)}
 * 配置类方式: {@link org.springframework.context.annotation.AnnotatedBeanDefinitionReader#register(Class[])}
 *
 * @author youyongkun
 * @date 2021/6/30 13:22
 */
@Import(AnnotationBeanDefinitionDemo.Config.class)
public class AnnotationBeanDefinitionDemo {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.register(AnnotationBeanDefinitionDemo.class);
        applicationContext.refresh();

        User user = applicationContext.getBean("user", User.class);
        System.out.println(user);


        applicationContext.close();
    }


    @Component
    public static class Config {
        @Bean
        public User user() {
            return new User();
        }
    }

}
